package com.sparsearr.datastructures.ztr.stack;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/4/17 20:54
 * @Description: TODO
 * @Version: 1.0
 */

public class Calculator {
    private static final Logger logger = LogManager.getLogger(Calculator.class);

    public static void main(String[] args) {
        // 根据前面的数字，操作符，数字的优先级进行比较
        String expression = "30+2*6-2";
        // 符号栈数栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);

        // 用于扫描
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        String keepNum = "";
        // 每次扫描的char保存到ch
        char ch;
        while (true) {
            // 依次得到expression 的 每一个字符
            ch = expression.substring(index, index + 1).charAt(0);
            // 判断是字符还是符号
            if (operStack.isOper(ch)) {
                // 如果是操作符，就直接入栈
                if (!operStack.isEmpty()) {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        // 将res 入数栈
                        numStack.push(res);
                        // 将符号入栈
                        operStack.push(ch);
                    } else {
                        // 直接入栈
                        operStack.push(ch);
                    }
                } else {
                    // 判断操作符栈是否为空,如果是空直接入栈
                    operStack.push(ch);
                }
            } else {
                // 如果是数字，就直接入栈
                // numStack.push(ch - 48);
                // 多位数的扫描，如果是多为数字的话，就需要再次扫描，而不是直接入栈，直到遇到符号才入栈
                keepNum += ch;
                // 如果当前扫描的是最后一个数字，就直接入栈
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        // 如果是操作符，就直接入栈
                        numStack.push(Integer.parseInt(keepNum));
                        // 清空keepNum
                        keepNum = "";
                    }
                }
            }
            // index 后移, 判断是否扫描到expression
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        // 当表达式扫描完毕，就顺序的从 数栈和符号栈中pop出相应的数和符号，并运行
        while (true) {
            // 如果符号栈为空，则计算到最后的结果，数栈中只有一个数字
            if (operStack.isEmpty()) {
                break;
            } else {
                num1 = numStack.pop();
                num2 = numStack.pop();
                oper = operStack.pop();
                res = numStack.cal(num1, num2, oper);
                numStack.push(res);
            }
        }
        // 将数栈的情况输出
        int res2 = numStack.pop();
        System.out.printf("表达式是 %s = %d", expression, res2);
    }
}

class ArrayStack2 {
    // 栈的大小
    private final int maxSize;

    // 数组，数据放在数组
    private final int[] stack;

    // 栈顶
    private int top = -1;

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    // 栈顶的值，不是pop出来
    public int peek() {
        return stack[top];
    }

    // 判断栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    // 判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    // 入栈
    public void push(int value) {
        // 判断栈是否满
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    // 出栈
    public int pop() {
        // 判断栈是否空
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    // 遍历栈，从栈顶开始依次输出
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    // 返回运算符的优先级，优先级是程序员来确定，优先级使用数字表示，数字越大，优先级越高
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    // 判断是不是一个运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    // 计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }

}